#include "adduserwidget.h"

AddUserWidget::AddUserWidget(QWidget *parent) : QWidget(parent)
{
       init_Widget();
       init_Control();
       init_Connect();
}
void AddUserWidget::init_Control()
{
    this->font.setPointSize(10);
    this->setFont(this->font);
    //文本框
    this->adduserlogNameLab=new QLabel();
    //this->adduserlogNameLab->setMaximumSize(100,70);
    this->adduserlogNameLab->setText(QString::fromUtf8("账号:"));
    this->adduserNameLab=new QLabel();
    //this->adduserNameLab->setMaximumSize(100,70);
    this->adduserNameLab->setText(QString::fromUtf8("姓名:"));
    this->adduserpsdLab=new QLabel();
    //this->adduserpsdLab->setMaximumSize(100,70);
    this->adduserpsdLab->setText(QString::fromUtf8("密码:"));
    this->adduserIdentityLab=new QLabel();
    //this->adduserIdentityLab->setMaximumSize(100,70);
    this->adduserIdentityLab->setText(QString::fromUtf8("身份:"));

    //文本框
    this->adduserlogNameEt=new QLineEdit();

    this->adduserNameEt=new QLineEdit();

    this->adduserpsdEt=new QLineEdit();

    this->adduserIdentityEt=new QLineEdit();


    this->addBtn=new QPushButton();
    this->addBtn->setText(QString::fromUtf8("确认"));
    //this->font.setPointSize(8);
    this->addBtn->setFont(this->font);
    this->returnBtn=new QPushButton();
    this->returnBtn->setText(QString::fromUtf8("返回"));
    //布局管理器
    this->vlayLab=new QVBoxLayout();
    this->vlayLab->addWidget(this->adduserlogNameLab);
    this->vlayLab->addWidget(this->adduserNameLab);
    this->vlayLab->addWidget(this->adduserpsdLab);
    this->vlayLab->addWidget(this->adduserIdentityLab);

    this->vlayEt=new QVBoxLayout();
    this->vlayEt->addWidget(this->adduserlogNameEt);
    this->vlayEt->addWidget(this->adduserNameEt);
    this->vlayEt->addWidget(this->adduserpsdEt);
    this->vlayEt->addWidget(this->adduserIdentityEt);

    this->hlay=new QHBoxLayout();
    this->hlay->addLayout(this->vlayLab);
    this->hlay->addLayout(this->vlayEt);

    this->hlayBtn=new QHBoxLayout();
    this->hlayBtn->addWidget(this->addBtn);
    this->hlayBtn->addWidget(this->returnBtn);

    this->vlay=new QVBoxLayout();
    this->vlay->addLayout(this->hlay);
    this->vlay->addLayout(this->hlayBtn);


    this->vlay->setAlignment(Qt::AlignCenter);

    this->setLayout(this->vlay);

}

void AddUserWidget::init_Widget()
{
    this->setWindowTitle(QString::fromUtf8("添加用户"));
    this->resize(800,600);

}

void AddUserWidget::init_Connect()
{
    connect(this->addBtn,SIGNAL(clicked()),this,SLOT(on_addBtn_cliked()));
    connect(this->returnBtn,SIGNAL(clicked()),this,SLOT(on_retutrnBtn_cliked()));
}

void AddUserWidget::on_addBtn_cliked()
{
    int ture;
    QString addlogUsername=this->adduserlogNameEt->text();
    QString addUsername=this->adduserNameEt->text();
    QString addUserpsd=this->adduserpsdEt->text();
    QString adduserIdentity=this->adduserIdentityEt->text();
    QString sql=QString("INSERT INTO Users(Name,user_jur,user_name,user_password) VALUES ('%1',%2,'%3','%4');").arg(addUsername).arg(adduserIdentity).arg(addlogUsername).arg(addUserpsd);
    if(addlogUsername==nullptr||addUsername==nullptr||addUserpsd==nullptr||adduserIdentity==nullptr)
    {
         QMessageBox::critical(this, "错误", "输入为空！！");
         return;
    }
    this->model=this->control->getInstance()->Creatmodel("user");
    ture=this->model->Table_Inserter((char*)sql.toStdString().c_str());
    if(ture)
    {
        QMessageBox::information(this,"成功","添加成功");
        this->adduserlogNameEt->clear();

        this->adduserNameEt->clear();

        this->adduserpsdEt->clear();

        this->adduserIdentityEt->clear();
        emit returnManager();
    }
    else
    {
     QMessageBox::critical(this, "错误", "添加错误");
    }
}

void AddUserWidget::on_retutrnBtn_cliked()
{
    emit returnManager();
}
